﻿@{
    ViewBag.Title = T("Admin.Configuration.Measures.Weights").Text;
}

<div class="section-header">
    <div class="title">
        <i class="fas fa-balance-scale"></i>
        <span>@T("Admin.Configuration.Measures.Weights")</span>
    </div>
</div>

<div>
	@(Html.Telerik().Grid<MeasureWeightModel>()
		.Name("measure-weight-grid")
		.DataKeys(x =>
		{
			x.Add(y => y.Id);
		})
		.Columns(columns =>
		{
			columns.Bound(x => x.Name)
				.Width(200);
			columns.Bound(x => x.SystemKeyword)
				.Width(150);
			columns.Bound(x => x.Ratio)
				.Format("{0:0.00000000}")
				.Width(180);
			columns.Bound(x => x.DisplayOrder)
				.Centered()
				.Width(100);
			columns.Bound(x => x.IsPrimaryWeight)
				.Width(100)
				.Centered()
				.ClientTemplate(Html.SymbolForBool("IsPrimaryWeight"));
			columns.Command(commands =>
			{
				commands.Custom("edit-measure-weight").Text(T("Common.Edit"));
				commands.Delete().Localize(T);
			}).Width(180)
			.HtmlAttributes(new { align = "right", @class = "omega" });
		})
		.ToolBar(commands => commands.Template(WeightGridCommands))
		.DataBinding(dataBinding =>
		{
			dataBinding.Ajax()
                .Select("Weights", "Measure")
				.Delete("DeleteWeight", "Measure");
		})
		.EnableCustomBinding(true))
</div>

@helper WeightGridCommands(Grid<MeasureWeightModel> grid)
{
	<button type="submit" id="btnAddNew" name="btnAddNew" class="btn" onclick="javascript:openPopup('@(Url.Action("CreateWeightPopup", "Measure", new { btnId = "btnRefresh" }))'); return false;">
		<i class="fa fa-plus"></i>
		<span>@T("Admin.Configuration.Measures.Weights.AddWeight")</span>
	</button>
	<input type="submit" id="btnRefresh" name="btnRefresh" class="d-none" />
}

<script>
	$(function() {
		$('#btnRefresh').click(function () {
			$('#measure-weight-grid').data('tGrid').ajaxRequest();
			return false;
		});

		$('#measure-weight-grid').on('click', '.t-grid-edit-measure-weight', function (e) {
			e.preventDefault();
			var tr = $(this).closest('tr');
			var id = $('#measure-weight-grid').data('tGrid').dataItem(tr).Id;
			var href = "@Url.Content("~/Admin/Measure/EditWeightPopup/")" + id + "?btnId=btnRefresh";
			
			openPopup(href);
			return false;
		});
	});
</script>
